home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Frameworks / Grant's CGI Framework 1.0b14 / Apple Events / AEHandlers.c < prev    next >
Text File  |  1996-04-11  |  5KB  |  223 lines

  1. /*****
  2.  *
  3.  *    AEHandlers.c
  4.  *
  5.  *    This is a support file for "Grant's CGI Framework".
  6.  *    Please see the license agreement that accompanies the distribution package
  7.  *    for licensing details.
  8.  *
  9.  *    Copyright ©1995,1996 by Grant Neufeld
  10.  *    grant@acm.com
  11.  *    http://arpp.carleton.ca/grant/
  12.  *
  13.  *****/
  14.  
  15. #include "MyConfiguration.h"
  16.  
  17. #include <AppleEvents.h>
  18.  
  19. #include "compiler_stuff.h"
  20. #include "constants.h"
  21. #include "globals.h"
  22.  
  23. #include "AEFunc.h"
  24. #include "ErrorUtil.h"
  25. #include "ProcessUtil.h"
  26. #include "Quit.h"
  27.  
  28. #include "AEHandlers.h"
  29.  
  30.  
  31. /***  REQUIRED SUITE  ***/
  32.  
  33. /* Sent if the application is opened by the Finder with no documents. */
  34. pascal OSErr
  35. doAEOpenApp ( AppleEvent *theAppleEvent, AppleEvent *reply, long refCon )
  36. {
  37.     /* You may want to add code here to handle the case where the application
  38.         is just opened with no other startup tasks (such as opening documents). */
  39.     
  40.     #if kCompileWithQuitOnLongIdle
  41.     if ( !(gDoIdleQuitOnOpenApp) )
  42.     {
  43.         /* the user opened the app (not a CGI event) so don't quit until the user
  44.             or another event specifies to do so */
  45.         gDoIdleQuit = false;
  46.     }
  47.     #endif
  48.     
  49.     return noErr;
  50. } /* doAEOpenApp */
  51.  
  52.  
  53. /* The application is expected to open the given documents, if possible */
  54. pascal OSErr
  55. doAEOpenDoc ( AppleEvent *theAppleEvent, AppleEvent *reply, long refCon )
  56. {
  57.     OSErr        theErr;
  58.     OSErr        docErr;
  59.     AEDescList    docList;
  60.     long        numDocs;
  61.     long        counter;
  62.     AEKeyword    keywd;
  63.     DescType    returnedType;
  64.     FSSpec        fileSpec;
  65.     Size        actualSize;
  66.     
  67.     docList.descriptorType    = typeNull;
  68.     docList.dataHandle        = NULL;
  69.     
  70.     /* get the direct parameter - a descriptor list - and put it into docList */
  71.     theErr = AEGetParamDesc ( theAppleEvent, keyDirectObject, typeAEList, &docList );
  72.     if ( theErr == noErr )
  73.     {
  74.         /* check for missing required parameters */
  75.         theErr = AEFuncGotRequiredParams ( theAppleEvent );
  76.         if ( theErr != noErr )
  77.         {
  78.             AEDisposeDesc ( &docList );
  79.         }
  80.     }
  81.     
  82.     if ( theErr == noErr )
  83.     {
  84.         /* count the number of descriptor records in the list.
  85.             Should be at least 1 since we got called and no error */
  86.         theErr = AECountItems ( &docList, &numDocs );
  87.         if ( theErr != noErr )
  88.         {
  89.             AEDisposeDesc ( &docList );
  90.         }
  91.     }
  92.     
  93.     if ( theErr == noErr )
  94.     {
  95.         /* now get each descriptor record from the list, coerce the returned data to an
  96.             FSSpec record, and open the associated file */
  97.         for ( counter = 1; counter <= numDocs; counter++ )
  98.         {
  99.             docErr = AEGetNthPtr ( &docList, counter, typeFSS, &keywd, &returnedType,
  100.                 (Ptr)&fileSpec, sizeof(fileSpec), &actualSize );
  101.             if ( docErr == noErr )
  102.             {
  103.                 ProcessIsMoreBusy ();
  104.                 docErr = CustomOpenDocument ( &fileSpec );
  105.                 ProcessIsLessBusy ();
  106.             }
  107.             
  108.             if ( docErr != noErr )
  109.             {
  110.                 theErr = docErr;
  111.             }
  112.         }
  113.     }
  114.     
  115.     if ( theErr == noErr )
  116.     {
  117.         theErr = AEDisposeDesc ( &docList );
  118.     }
  119.     
  120.     return theErr;
  121. } /* doAEOpenDoc */
  122.  
  123.  
  124. /* Handle document printing */
  125. pascal OSErr
  126. doAEPrintDoc ( AppleEvent *theAppleEvent, AppleEvent *reply, long refCon )
  127. {
  128.     return noErr;
  129. } /*  */
  130.  
  131.  
  132. /* Set application to quit. IM:IAC 9-8 */
  133. pascal OSErr
  134. doAEQuitApp ( AppleEvent *theAppleEvent, AppleEvent *reply, long refCon )
  135. {
  136.     OSErr        theErr;
  137.     Boolean        success;
  138.     
  139.     /* check for missing required parameters */
  140.     theErr = AEFuncGotRequiredParams ( theAppleEvent );
  141.     if ( theErr == noErr)
  142.     {
  143.         success = QuitPrepare ( true );
  144.     }
  145.     else
  146.     {
  147.         success = false;
  148.     }
  149.     
  150.     /* return error result */
  151.     if ( success == true )
  152.     {
  153.         return noErr;
  154.     }
  155.     else if ( theErr == noErr )
  156.     {
  157.         return userCanceledErr;
  158.     }
  159.     else
  160.     {
  161.         return theErr;
  162.     }
  163. } /* doAEQuitApp */
  164.  
  165.  
  166. /**  INITIALIZATION **/
  167. #pragma mark -
  168.  
  169. #pragma segment Startup
  170. /* We don't track or dispose of theUPPs created in this function because they
  171.     remain in use until the application quits, at which point they are
  172.     automatically released when all of the app's memory is released. */
  173. OSErr
  174. AEInstallHandlers ( void )
  175. {
  176.     OSErr                resultErr;
  177.     OSErr                theErr;
  178.     AEEventHandlerUPP    theUPP;
  179.  
  180.     resultErr = noErr;
  181.     
  182.     /* NewAEEventHandlerProc as discussed in
  183.         "Odds and Ends" by Dave Mark, in 'MacTech' Aug94.
  184.         Revised usage in "The Required Apple Events" by Dave Mark, in 'MacTech' Dec94. */
  185.     theUPP    = NewAEEventHandlerProc ( doAEOpenApp );
  186.     theErr    = AEInstallEventHandler ( kCoreEventClass, kAEOpenApplication, theUPP, 0L, false );
  187.     if ( theErr != noErr )
  188.     {
  189.         /* openapp AE install failed */
  190.         resultErr = theErr;
  191.     }
  192.     
  193.     theUPP    = NewAEEventHandlerProc ( doAEOpenDoc );
  194.     theErr    = AEInstallEventHandler ( kCoreEventClass, kAEOpenDocuments, theUPP, 0L, false );
  195.     if ( theErr != noErr )
  196.     {
  197.         /* opendoc AE install failed */
  198.         resultErr = theErr;
  199.     }
  200.  
  201.     theUPP    = NewAEEventHandlerProc ( doAEPrintDoc );
  202.     theErr    = AEInstallEventHandler ( kCoreEventClass, kAEPrintDocuments, theUPP, 0L, false );
  203.     if ( theErr != noErr )
  204.     {
  205.         /* printdoc AE install failed */
  206.         resultErr = theErr;
  207.     }
  208.     
  209.     theUPP    = NewAEEventHandlerProc ( doAEQuitApp );
  210.     theErr    = AEInstallEventHandler ( kCoreEventClass, kAEQuitApplication, theUPP, 0L, false );
  211.     if ( theErr != noErr )
  212.     {
  213.         /* quit app AE install failed */
  214.         resultErr = theErr;
  215.     }
  216.     
  217.     return resultErr;
  218. } /* AEInstallHandlers */
  219. #pragma segment AppleEvents
  220.  
  221.  
  222. /*****  EOF  *****/
  223.